< previous page page_300 next page >

Page 300
VARIABLE AVARIABLE BRESULT (A XOR B)
FalseFalseFalse
FalseTrueTrue
TrueFalseTrue
TrueTrueFalse
The XOR Operation

You can verify this by running the Boolean.vbp sample project found in the Tutorial Samples directory on the CD-ROM that came with this book.
When you do, just click on the label controls to toggle A and B from True to False and back (don't uncheck the "Use Boolean Variables" checkbox for now).
Booleans and Numbers
The Boolean operations described so far operate on variables that have a True or False value. Visual Basic variables that are declared as Boolean can have only one of these two values.
Consider this simple program:
Dim A As Boolean
Dim B As Integer
A = 5
Debug.Print A
B = A
Debug.Print B
When you assign the Boolean variable A a non-zero value (5), it is set to True. The first Debug.Print statement will display "True." When you assign A to a numeric variable, the value assigned is 1.
This raises an interesting question. If any non-zero value is True, why use 1?
Believe it or not, this question is not just a narrative tool to help make this part of the book more entertaining. If you don't understand the answer to this question, there is an excellent chance that you will inadvertently add subtle bugs to many of your VB programs that use API functions, the kind of infuriating bugs that can take hours to figure out.
To answer that question, we have to back up and ask this question:
You know how Boolean operations work on Boolean variables based on the truth tables shown earlier in this tutorial. How then, do they work on numeric variables?
A numeric variable is made up of bits (as described in Tutorial 2, "Memory, Where It All Begins"). Byte variables have 8 bits, integers have 16 bits, and longs

 
< previous page page_300 next page >